<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>

    <script type="text/javascript">

    function test_toFilter(t) {
        t.plan(27);

        /*
         * Set up
         */

        var form, filter, fields = [];

        fields.push(new Ext.form.TextField({
            name: "foo0",
            value: "bar0"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo1__eq",
            value: "bar1"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo2__ne",
            value: "bar2"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo3__lt",
            value: "bar3"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo4__le",
            value: "bar4"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo5__gt",
            value: "bar5"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo6__ge",
            value: "bar6"
        }));

        fields.push(new Ext.form.TextField({
            name: "foo7__like",
            value: "bar7"
        }));

        form = new Ext.form.FormPanel({
            renderTo: "form",
            items: fields
        });

        /*
         * Test
         */

        // 26 tests
        filter = GeoExt.form.toFilter(form);

        t.ok(filter instanceof OpenLayers.Filter.Logical,
             "GeoExt.form.filterFormForm returns a logical filter");

        t.eq(filter.type, OpenLayers.Filter.Logical.AND,
             ["GeoExt.form.filterFormForm returns a logical AND filter if",
              "logicalOp is undefined"].join(" "));

        t.eq(filter.filters[0].type, OpenLayers.Filter.Comparison.EQUAL_TO,
             "GeoExt.form.filterFormForm creates correct filter type");
        t.eq(filter.filters[0].property, "foo0",
             "GeoExt.form.filterFormForm creates correct filter prop");
        t.eq(filter.filters[0].value, "bar0",
             "GeoExt.form.filterFormForm creates correct filter value");

        t.eq(filter.filters[1].type, OpenLayers.Filter.Comparison.EQUAL_TO,
             "GeoExt.form.filterFormForm creates correct filter type (__eq)");
        t.eq(filter.filters[1].property, "foo1",
             "GeoExt.form.filterFormForm creates correct filter prop (__eq)");
        t.eq(filter.filters[1].value, "bar1",
             "GeoExt.form.filterFormForm creates correct filter value (__eq)");

        t.eq(filter.filters[2].type, OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
             "GeoExt.form.filterFormForm creates correct filter type (__ne)");
        t.eq(filter.filters[2].property, "foo2",
             "GeoExt.form.filterFormForm creates correct filter prop (__ne)");
        t.eq(filter.filters[2].value, "bar2",
             "GeoExt.form.filterFormForm creates correct filter value (__ne)");

        t.eq(filter.filters[3].type, OpenLayers.Filter.Comparison.LESS_THAN,
             "GeoExt.form.filterFormForm creates correct filter type (__lt)");
        t.eq(filter.filters[3].property, "foo3",
             "GeoExt.form.filterFormForm creates correct filter prop (__lt)");
        t.eq(filter.filters[3].value, "bar3",
             "GeoExt.form.filterFormForm creates correct filter value (__lt)");

        t.eq(filter.filters[4].type, OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO,
             "GeoExt.form.filterFormForm creates correct filter type (__le)");
        t.eq(filter.filters[4].property, "foo4",
             "GeoExt.form.filterFormForm creates correct filter prop (__le)");
        t.eq(filter.filters[4].value, "bar4",
             "GeoExt.form.filterFormForm creates correct filter value (__le)");

        t.eq(filter.filters[5].type, OpenLayers.Filter.Comparison.GREATER_THAN,
             "GeoExt.form.filterFormForm creates correct filter type (__gt)");
        t.eq(filter.filters[5].property, "foo5",
             "GeoExt.form.filterFormForm creates correct filter prop (__gt)");
        t.eq(filter.filters[5].value, "bar5",
             "GeoExt.form.filterFormForm creates correct filter value (__gt)");

        t.eq(filter.filters[6].type, OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
             "GeoExt.form.filterFormForm creates correct filter type (__ge)");
        t.eq(filter.filters[6].property, "foo6",
             "GeoExt.form.filterFormForm creates correct filter prop (__ge)");
        t.eq(filter.filters[6].value, "bar6",
             "GeoExt.form.filterFormForm creates correct filter value (__ge)");

        t.eq(filter.filters[7].type, OpenLayers.Filter.Comparison.LIKE,
             "GeoExt.form.filterFormForm creates correct filter type (__like)");
        t.eq(filter.filters[7].property, "foo7",
             "GeoExt.form.filterFormForm creates correct filter prop (__like)");
        t.eq(filter.filters[7].value, "bar7",
             "GeoExt.form.filterFormForm creates correct filter value (__like)");

        // 1 test
        filter = GeoExt.form.toFilter(form,
                                      OpenLayers.Filter.Logical.OR);

        t.eq(filter.type, OpenLayers.Filter.Logical.OR,
             ["GeoExt.form.filterFormForm returns a logical OR filter if",
              "logicalOp is OpenLayers.Filter.Logical.OR"].join(" "));
    }

    function test_toFilter_Wildcard(t) {
        t.plan(4);

        /*
         * Set up
         */

        var form, filter, fields = [];

        fields.push(new Ext.form.TextField({
            name: "foo__like",
            value: "bar"
        }));

        form = new Ext.form.FormPanel({
            renderTo: "form",
            items: fields
        });

        /*
         * Test
         */

        filter = GeoExt.form.toFilter(form, null, GeoExt.form.STARTS_WITH);
        t.eq(filter.filters[0].value, "bar.*", "GeoExt.form.STARTS_WITH appends a wildcard");
        filter = GeoExt.form.toFilter(form, null, GeoExt.form.ENDS_WITH);
        t.eq(filter.filters[0].value, ".*bar", "GeoExt.form.ENDS_WITH prepends a wildcard");
        filter = GeoExt.form.toFilter(form, null, GeoExt.form.CONTAINS);
        t.eq(filter.filters[0].value, ".*bar.*", "GeoExt.form.CONTAINS appends *and* prepends a wildcard");
        filter = GeoExt.form.toFilter(form);
        t.eq(filter.filters[0].value, "bar", "Default behaviour is to not add any wildcards");
    }

    </script>
  <body>
    <div id="form"></div>
  </body>
</html>
